<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * PHP version 5
 *
 * Copyright 2008 The Picnic PHP Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @category      Framework
 * @package       Pfw
 * @author        Sean Sitter <sean@picnicphp.com>
 * @copyright     2008 The Picnic PHP Framework
 * @license       http://www.apache.org/licenses/LICENSE-2.0
 * @link          http://www.picnicphp.com
 * @since         0.10
 */

function smarty_function_form_field($params, &$smarty) 
{
    list($args, $attrs) = $smarty->_filterAttrs($params);
    
    $obj_prop = $args['prop'];
    if ($args['confirmation']) {
        $obj_prop = $obj_prop . "_confirm";
    }
    $obj_name = $args['for'];
    if (empty($obj_name)) {
        error_log("missing required template variable name 'for'");
    }
    $obj = $smarty->get_template_vars($obj_name);
    if (!is_object($obj)) {
        error_log("invalid template object: '{$obj_name}'");
        return null; 
    }
    
    $element = "input";
    $content = null;
    
    if(!isset($args['type'])) {
        if (method_exists($obj, 'getSchema')) {
            $schema = $obj->getSchema();
            switch($schema[$obj_prop]['type'])
            {
                case Pfw_Db_Adapter::TYPE_CHARACTER:
                case Pfw_Db_Adapter::TYPE_INTEGER:
                case Pfw_Db_Adapter::TYPE_FLOAT:
                case Pfw_Db_Adapter::TYPE_ENUM:
                    $attrs['type'] = "text";
                    if (!isset($attrs['size']))
                        $attrs['size'] = 3;
                    break;
                case Pfw_Db_Adapter::TYPE_TEXT:
                    $attrs['type'] = "textarea";
                    break;
                case Pfw_Db_Adapter::TYPE_VARCHAR:
                    $attrs['type'] = "text";
                    if (!isset($attrs['size']))
                        $attrs['size'] = 32;
                    break;
                case Pfw_Db_Adapter::TYPE_DATE:
                    break;
                case Pfw_Db_Adapter::TYPE_TIME:
                    break;
            }
        }
        else {
            $attrs['type'] = "text";
        }
    }
    else {
        $attrs['type'] = $args['type'];
    }
    
    if (!empty($obj->$obj_prop)) {
        $attrs['value'] = $obj->$obj_prop;
    } 
    elseif (empty($attrs['value'])) {
        $attrs['value'] = "";
    }
    
    if ('textarea' == $attrs['type']) {
        $element = "textarea";
        $content = $attrs['value'];
        unset($attrs['type']);
        unset($attrs['value']);
    }
    elseif ('select' == $attrs['type']) {
        $element = "select";
        $content = "\n";
        if (empty($params['options'])) {$params['options'] = array();}
        $options = array_to_hash($params['options']);
        foreach ($options as $label => $value) {
            $label = empty($label) ? $value : $label;
            $selected = "";
            if ($value == $attrs['value']) {
                $selected = " selected";
            }
            $value = urlencode($value);
            $content .= "<option value=\"{$value}\"{$selected}>$label</option>\n";
        }
        unset($attrs['type']);
        unset($attrs['value']);
    }
    
    $attrs['name'] = "{$obj_name}[$obj_prop]";
    
    $err_list = "";
    if ($obj->hasErrors($args['prop']) and (false !== $args['highlight_errors'])) {
        if (isset($attrs['class'])) {
            $attrs['class'] = "{$attrs['class']} pfw-error";
        }
        else {
            $attrs['class'] = "pfw-error";
        }
    }
    
    $field = $smarty->_generateElement($element, $attrs, $content);
    
    return $err_list . $field;
}
